package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 工作台相关接口
 */
@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private UserMapper userMapper ;
    @Autowired
    private OrderMapper orderMapper ;
    @Autowired
    private SetmealMapper setmealMapper ;
    @Autowired
    private DishMapper dishMapper ;

    /**
     * 获取工作台今日数据
     *
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime beginTime , LocalDateTime endTime) {


        //封装 map 集合
        Map mapForUser = new HashMap<>() ;
        mapForUser.put("begin",beginTime) ;
        mapForUser.put("end",endTime) ;

        //获取今日新增用户数量
        Integer newUsers =userMapper.getNewUserByDate(mapForUser);

        //获取当日总订单数量
        //封装 map 集合
        Map mapForTotalOrder = new HashMap<>() ;
        mapForTotalOrder.put("begin",beginTime) ;
        mapForTotalOrder.put("end",endTime) ;

        Integer totalOrder = orderMapper.countByMap(mapForTotalOrder);

        //获取当日有效订单数量
        //封装 map 集合
        Map mapForValidTotalOrder = new HashMap<>() ;
        mapForValidTotalOrder.put("begin",beginTime) ;
        mapForValidTotalOrder.put("end",endTime) ;
        mapForTotalOrder.put("status",Orders.COMPLETED) ;

        Integer totalValidOrder = orderMapper.countByMap(mapForTotalOrder);

        //计算订单完成率
        Double orderCompletionRate = totalOrder.doubleValue() == 0 ? 0.0 : totalValidOrder.doubleValue() / totalOrder.doubleValue() ;

        //统计营业额
        Map mapForTurnover = new HashMap<>() ;
        mapForTurnover.put("begin",beginTime) ;
        mapForTurnover.put("end",endTime) ;
        mapForTurnover.put("status",Orders.COMPLETED) ;

        //获取营业额
        Double turnover = orderMapper.getTurnoverByMap(mapForTurnover) == null ? (Double) 0.0 : orderMapper.getTurnoverByMap(mapForTurnover) ;

        //计算每一单的平均价格
        Double unitPrice = turnover == 0.0 ? 0.0 : turnover.doubleValue() / totalValidOrder.doubleValue() ;

        //封装响应结果
        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .validOrderCount(totalValidOrder)
                .turnover(turnover)
                .unitPrice(unitPrice)
                .build();
        return businessDataVO;
    }

    /**
     * 工作台套餐信息统计
     *
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        //查询起售中的套餐数量
        Integer sold = setmealMapper.countByStatus(StatusConstant.ENABLE) ;
        //查询停售中的套餐数量
        Integer discontinued = setmealMapper.countByStatus(StatusConstant.DISABLE) ;

        //封装响应结果
        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
        return setmealOverViewVO;
    }

    /**
     * 工作台菜品信息统计
     *
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        //统计起售中的菜品数量
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE) ;
        //统计停售中的菜品数量
        Integer discontinued = dishMapper.countByStatus(StatusConstant.DISABLE) ;

        //封装响应结果
        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
        return dishOverViewVO;
    }

    /**
     * 工作台订单管理信息统计
     *
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        //查询全部订单数量
        Integer allOrders = orderMapper.countByStatus(null) ;
        //查询已取消订单的数量
        Integer cancelledOrders = orderMapper.countByStatus(Orders.CANCELLED) ;
        //查询已完成订单的数量
        Integer completedOrders = orderMapper.countByStatus(Orders.COMPLETED) ;
        //查询待派送订单的数量
        Integer deliveredOrders = orderMapper.countByStatus(Orders.CONFIRMED) ;
        //查询待接单订单的数量
        Integer waitingOrders = orderMapper.countByStatus(Orders.TO_BE_CONFIRMED) ;

        //封装响应结果
        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
        return orderOverViewVO;
    }
}
